﻿#ifndef HNRJITCONTROL_H
#define HNRJITCONTROL_H

#include "HnrjitWigdet.h"

class CHnrjitContainer;

class DIRECTUI_API CHnrjitControl : public CHnrjitWigdet
{
public:
	/// <summary>控件状态枚举</summary>
	enum ControlStatus
	{
		kNormal = 0, // 控件正常状态
		kActivated, // 控件激活状态
		kDisabled, // 控件禁用状态
	};

	typedef CHnrjitControl::ControlStatus ControlStatus; // 方便子类引用

	typedef void (CHnrjitControl::*SetXxxColor)(CHnrjitControl::ControlStatus, COLORREF);

public:
	explicit CHnrjitControl();
	virtual ~CHnrjitControl();

	/// <summary>获取当前类标识</summary>
	virtual DWORD GetClsid() const { return CLSID_HNRJITCONTROL; }

	/// <summary>设置属性值</summary>
	/// <param name="pxElement">XML元素</param>
	virtual void SetAttribute(const TiXmlElement* pxElement, HZIP hZipRes = NULL);

public:
	/// <summary>获取控件状态</summary>
	/// <returns>see summary</returns>
	virtual ControlStatus GetControlState() const;

	/// <summary>控件文本属性</summary>
	virtual const CAtlString& GetTitle() const;
	virtual void SetTitle(const TCHAR* pszTitle);

	/// <summary>控件文本对齐属性</summary>
	virtual UINT GetTextAlign() const;
	virtual void SetTextAlign(UINT nTextAlign);

	/// <summary>控件字体属性</summary>
	virtual BOOL GetFont(LOGFONT* pLogFont);
	virtual void SetFont(LOGFONT* pLogFont);
	virtual void SetFont(int nFontSize, const TCHAR* pszFontName, int nWeight = FW_NORMAL,
		BYTE bItalic = FALSE, BYTE bUnderline = FALSE, BYTE bStrikeOut = FALSE, HDC hDC = NULL);

	/// <summary>控件背景颜色属性</summary>
	virtual COLORREF GetBackColor(ControlStatus iControlStatus) const;
	virtual void SetBackColor(ControlStatus iControlStatus, COLORREF crBackColor);
	void SetBackColor(COLORREF crBackColor);

	/// <summary>控件字体颜色属性</summary>
	virtual COLORREF GetForeColor(ControlStatus iControlStatus) const;
	virtual void SetForeColor(ControlStatus iControlStatus, COLORREF crForeColor);
	void SetForeColor(COLORREF crForeColor);

	/// <summary>控件边框颜色属性</summary>
	virtual COLORREF GetBorderColor(ControlStatus iControlStatus) const;
	virtual void SetBorderColor(ControlStatus iControlStatus, COLORREF crBorderColor);
	virtual void SetBorderColor(COLORREF crBorderColor);

	/// <summary>控父件容器属性</summary>
	virtual CHnrjitContainer* GetContainer() const;
	virtual void SetContainer(CHnrjitContainer* pContainer);

	/// <summary>控件状态图片属性</summary>
	virtual CComPtr<IImage> GetBackIcon(ControlStatus iControlStatus) const;
	virtual void SetBackIcon(ControlStatus iControlStatus, CComPtr<IImage> spBackIcon);

	/// <summary>自动调整控件大小</summary>
	/// <param name="iControlStatus">控件状态</param>
	virtual void AutoSize(ControlStatus iControlStatus);

public:
	/// <summary>绘制控件</summary>
	/// <param name="pDC">设备上下文</param>
	virtual void DrawControl(HDC hdc) = 0;

	/// <summary>控件单击事件</summary>
	/// <remarks>建议重载</remarks>
	virtual void OnClicked();

	/// <summary>鼠标按下事件</summary>
	/// <param name="nFlags">按下的键</param>
	/// <param name="point">鼠标位置</param>
	virtual void OnMousePress(UINT nFlags, CPoint point);

	/// <summary>鼠标释放事件</summary>
	/// <param name="nFlags">按下的键</param>
	/// <param name="point">鼠标位置</param>
	virtual void OnMouseRelease(UINT nFlags, CPoint point);

	/// <summary>激活控件事件</summary>
	/// <param name="pOldControl">之前激活的控件</param>
	virtual void OnSetFocus(CHnrjitControl* pOldControl) {};

	/// <summary>释放控件事件</summary>
	/// <param name="pNewControl">当前激活的控件</param>
	virtual void OnKillFocus(CHnrjitControl* pNewControl) {};

protected:
	/// <summary>请求重绘控件</summary>
	/// <param name="rcRect">要重绘区域</param>
	virtual void Invalidate(const CRect& rcRect);

	/// <summary>绘制文本</summary>
	/// <param name="pDC">设备上下文</param>
	/// <param name="rcGeometry">要渲染区域</param>
	/// <param name="iControlStatus">控件状态</param>
	virtual void DrawTitle(HDC hdc, const CRect& rcGeometry, ControlStatus iControlStatus);

	/// <summary>绘制矩形</summary>
	/// <param name="pDC">设备上下文</param>
	/// <param name="rcGeometry">要渲染区域</param>
	/// <param name="iControlStatus">控件状态</param>
	virtual void DrawRect(HDC hdc, const CRect& rcGeometry, ControlStatus iControlStatus);

	/// <summary>绘制图片</summary>
	/// <param name="pDC">设备上下文</param>
	/// <param name="rcGeometry">要渲染区域</param>
	/// <param name="iControlStatus">控件状态</param>
	virtual void DrawImage(HDC hdc, const CRect& rcGeometry, ControlStatus iControlStatus);

	friend class CHnrjitContainer; // 允许容器类访问私有成员

protected:
	CAtlString m_strTitle; // 控件文本

	UINT m_nTextAlign; // 控件文本对齐

	HGDIOBJ m_tagFont; // 控件文本字体

	BOOL m_isTriggered; // 是否可触发单击事件

	COLORREF m_crBackColors[3]; // 控件背景画刷

	COLORREF m_crForeColors[3]; // 控件文本颜色

	COLORREF m_3dBorderColors[3]; // 控件边缘颜色

	CComPtr<IImage> m_spBackIcons[3]; // 控件状态图片

	CHnrjitContainer* m_pContainer; // 控件父容器
};

#endif // HNRJITCONTROL_H